import 'api_response.dart';

/// 国标通道模型
class GBChannel {
  final int id;
  final String deviceId; // 国标通道ID
  final String name;
  final String? manufacturer;
  final int dataDeviceId; // 所属设备主键ID
  final String status;
  final String channelId;
  final String? createTime;
  final String? updateTime;
  final String? civilCode;
  final String? address;
  final String? parentId;
  final String? safetyWay;
  final String? registerWay;
  final String? certNum;
  final String? certifiable;
  final String? errCode;
  final String? secrecy;
  final String? ipAddress;
  final int? port;
  final String? password;
  final int? ptzType; // 云台类型：1-球机, 2-半球, 3-固定枪机, 4-遥控枪机
  final String? streamId;
  final String? customType;
  final String? longitude;
  final String? latitude;
  final String? ptz;
  final String? subCount;
  final String? parental;
  final String? ptztModel;
  final int hasAudio; // 0: 无音频, 1: 有音频
  final String? model;  // 设备型号
  final String? owner;  // 所有者
  final int? channelType; // 通道类型

  GBChannel({
    this.id = 0,
    required this.deviceId,
    required this.name,
    this.manufacturer,
    required this.dataDeviceId,
    this.status = '',
    required this.channelId,
    this.createTime,
    this.updateTime,
    this.civilCode,
    this.address,
    this.parentId,
    this.safetyWay,
    this.registerWay,
    this.certNum,
    this.certifiable,
    this.errCode,
    this.secrecy,
    this.ipAddress,
    this.port,
    this.password,
    this.ptzType,
    this.streamId,
    this.customType,
    this.longitude,
    this.latitude,
    this.ptz,
    this.subCount,
    this.parental,
    this.ptztModel,
    this.hasAudio = 0,
    this.model,
    this.owner,
    this.channelType,
  });

  factory GBChannel.fromJson(Map<String, dynamic> json) {
    try {
      return GBChannel(
        id: json['id'] is int ? json['id'] : int.tryParse(json['id']?.toString() ?? '0') ?? 0,
        deviceId: json['deviceId']?.toString() ?? '',
        name: json['name']?.toString() ?? '',
        channelId: json['channelId']?.toString() ?? json['deviceId']?.toString() ?? '',
        manufacturer: json['manufacturer']?.toString(),
        dataDeviceId: json['gbDeviceId'] is int 
            ? json['gbDeviceId'] 
            : int.tryParse(json['gbDeviceId']?.toString() ?? '0') ?? 0,
        status: json['status']?.toString() ?? 'UNKNOWN',
        createTime: json['createTime']?.toString(),
        updateTime: json['updateTime']?.toString(),
        civilCode: json['civilCode']?.toString(),
        address: json['address']?.toString(),
        parentId: json['parentId']?.toString(),
        safetyWay: json['safetyWay']?.toString(),
        registerWay: json['registerWay']?.toString(),
        certNum: json['certNum']?.toString(),
        certifiable: json['certifiable']?.toString(),
        errCode: json['errCode']?.toString(),
        secrecy: json['secrecy']?.toString(),
        ipAddress: json['ipAddress']?.toString(),
        port: json['port'] is int ? json['port'] : int.tryParse(json['port']?.toString() ?? '0'),
        password: json['password']?.toString(),
        ptzType: json['ptzType'] is int 
            ? json['ptzType'] 
            : int.tryParse(json['ptzType']?.toString() ?? '0'),
        streamId: json['streamId']?.toString(),
        customType: json['customType']?.toString(),
        longitude: json['longitude']?.toString(),
        latitude: json['latitude']?.toString(),
        ptz: json['ptz']?.toString(),
        subCount: json['subCount']?.toString(),
        parental: json['parental']?.toString(),
        ptztModel: json['ptztModel']?.toString(),
        hasAudio: json['hasAudio'] is int 
            ? json['hasAudio'] 
            : int.tryParse(json['hasAudio']?.toString() ?? '0') ?? 0,
        model: json['model']?.toString(),
        owner: json['owner']?.toString(),
        channelType: json['channelType'] is int 
            ? json['channelType'] 
            : int.tryParse(json['channelType']?.toString() ?? '0'),
      );
    } catch (e) {
      // 如果解析过程中出错，返回一个最小化的有效对象
      print('Error parsing GBChannel: $e');
      return GBChannel(
        id: 0,
        deviceId: json['deviceId']?.toString() ?? '',
        name: json['name']?.toString() ?? 'Unknown Channel',
        channelId: json['channelId']?.toString() ?? json['deviceId']?.toString() ?? '',
        dataDeviceId: 0,
        status: 'UNKNOWN',
      );
    }
  }
}

/// 国标通道列表响应
class GBChannelListResponse {
  final int total;
  final List<GBChannel> rows;
  final int code;
  final String msg;

  GBChannelListResponse({
    required this.total,
    required this.rows,
    required this.code,
    required this.msg,
  });

  factory GBChannelListResponse.fromJson(Map<String, dynamic> json) {
    return GBChannelListResponse(
      total: json['total'] ?? 0,
      rows: (json['rows'] as List?)?.map((e) => GBChannel.fromJson(e)).toList() ?? [],
      code: json['code'] ?? 500,
      msg: json['msg'] ?? 'Unknown error',
    );
  }

  bool get isSuccess => code == 200;
}

/// 国标通道详情响应
class GBChannelDetailResponse extends ApiResponse<GBChannel> {
  GBChannelDetailResponse({
    required int code,
    required String msg,
    GBChannel? data,
  }) : super(code: code, msg: msg, data: data);

  factory GBChannelDetailResponse.fromJson(Map<String, dynamic> json) {
    return GBChannelDetailResponse(
      code: json['code'] ?? 500,
      msg: json['msg'] ?? 'Unknown error',
      data: json['data'] != null ? GBChannel.fromJson(json['data']) : null,
    );
  }
} 